const http = require('http');
const fs = require('fs');
const path = require('path');

let server = http.createServer((req, res) => {
    // 1.0 读取index.html页面
    let { url } = req;
    if (url === '/index.html') {
        let htmlString = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8');
        res.setHeader('content-type', 'text/html;charset=utf8');
        res.end(htmlString);
    } else if (url === '/style.css') {
        let cssString = fs.readFileSync(path.join(__dirname, 'style.css'), 'utf-8');
        res.setHeader('content-type', 'text/css;charset=utf8');
        res.end(cssString);
    } else if (url === '/images/bg.jpg') {
        let img = fs.readFileSync(path.join(__dirname, '/images/bg.jpg'));
        res.setHeader('content-type', 'image/jpg;charset=utf8');
        res.end(img);
    }
    else if (url === '/images/fm.jpg') {
        let img = fs.readFileSync(path.join(__dirname, '/images/fm.jpg'));
        res.setHeader('content-type', 'image/jpg;charset=utf8');
        res.end(img);
    } else {
        res.statusCode = 404;
        res.setHeader('content-type', 'text/html;charset=utf8');
        res.end('资源没有找到');
    }
})

server.listen(8002, () => {
    console.log('启动');
})